package tests;

import common.AutoTestUtil;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;

import java.io.IOException;

public class PageDetail extends AutoTestUtil {
    /**
     * 文章详情页面
     */

    public void successDetail() throws NoSuchMethodException, IOException, InterruptedException {
        //在进行文章发布编辑测试后，点击文章进入文章页面
        WebElement element = driver.findElement(By.cssSelector("#artical-items-body > div:nth-child(1) > div > div.col > div.text-truncate > a > strong"));
        String titleName = element.getText();
        //点击进入页面
        element.click();
        //此时进行判断是否登录成功
        try {
            String targetName = driver.findElement(By.cssSelector("#details_article_title")).getText();
            if(titleName.equals(targetName)){
                System.out.println("进入博客详情页面成功");
            }
            getScreen(this.getClass().getMethod("successDetail").getName());
        }catch (Exception e){
            System.out.println("successDeatil方法出现异常：" + e);
        }

        //记录原本的点赞次数
        String count1 = driver.findElement(By.cssSelector("#details_article_likeCount")).getText();
        //判断点赞操作是否正常
        //可能会存在元素找不到的异常
        element = driver.findElement(By.cssSelector("#details_btn_like_count"));
        //绕过前端检测直接点击
        driver.executeScript("arguments[0].click();", element);

        //判断点赞是否成功
        String count2 = driver.findElement(By.cssSelector("#details_article_likeCount")).getText();
        if(!count1.equals(count2)){
            //说明这里的点赞成功了
            System.out.println("点赞操作成功");
        }


        //进行回复操作测试
        //直接回复,绕过前端检查，直接进行点击操作
        element = driver.findElement(By.cssSelector("#details_btn_article_reply"));
        JavascriptExecutor executor = (JavascriptExecutor) driver;
        executor.executeScript("arguments[0].click();", element);


        element = driver.findElement(By.cssSelector("#article_details_reply > div.CodeMirror.cm-s-default.CodeMirror-wrap.CodeMirror-empty > div.CodeMirror-scroll > div.CodeMirror-sizer > div > div > div > div.CodeMirror-code > div > pre"));
        //模拟鼠标
        Actions actions = new Actions(driver);
        actions.doubleClick(element).perform();
        actions.moveToElement(element).sendKeys("这是一篇好文章").perform();

        getScreen(this.getClass().getMethod("successDetail").getName());

        //这里可能会出现没有输入就进行点击回复的情况，所以需要进行强制等待
        Thread.sleep(1000);
        //再次进行提交操作
        element = driver.findElement(By.cssSelector("#details_btn_article_reply"));
        executor.executeScript("arguments[0].click();", element);

        getScreen(this.getClass().getMethod("successDetail").getName());

        Thread.sleep(2000);
        //进行删除帖子的测试
        element = driver.findElement(By.cssSelector("#bit-forum-content > div.page-body > div > div > div:nth-child(1) > div.col-9.card.card-lg > div.card-footer.bg-transparent.mt-auto.justify-content-end > div > div:nth-child(3) > div > a"));
        //移动到元素位置
        actions.moveToElement(element).perform();
        element.click();
        Thread.sleep(1000);
        //点击取消操作
        element = driver.findElement(By.cssSelector("#details_delete_modal > div > div > div.modal-footer > div > div > div:nth-child(1) > a"));
        executor.executeScript("arguments[0].click();", element);

        getScreen(this.getClass().getMethod("successDetail").getName());
        //进行等待
        Thread.sleep(1000);
        //再次点击删除
        driver.findElement(By.cssSelector("#bit-forum-content > div.page-body > div > div > div:nth-child(1) > div.col-9.card.card-lg > div.card-footer.bg-transparent.mt-auto.justify-content-end > div > div:nth-child(3) > div > a")).click();
        //点击确定操作
        driver.findElement(By.cssSelector("#details_artile_delete")).click();

        //判断是否成功删除，即判断是否进入了首页
        try {
            driver.findElement(By.cssSelector("#article_list_board_title"));
            System.out.println("删除操作成功");
            getScreen(this.getClass().getMethod("successDetail").getName());
        }catch (Exception e){
            System.out.println("successDetail方法出现异常：" + e);
        }
    }
}
